package models;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Id;

import com.avaje.ebean.Ebean;
import com.avaje.ebean.ExpressionList;
import com.avaje.ebean.RawSql;

import play.db.ebean.Model;
import search.ISearchable;
import models.edu_tags;

@Entity
public class edu_courses extends Model implements ISearchable{

	@Id
	public int course_id;
	
	public String name;
		
	public String code;
	
	public int institution_id;	
	
	public int instructor_id;
	
	public long start_date;
	
	public long end_date;
	
	public String description;
	
	public String short_description;
	
	public int is_active;
	
	public int parent_id;
	
	public int created_by;
	
	public long creation_date;
	
	public String url;
	
	public static Finder<Long,edu_courses> find = new Finder<Long,edu_courses>(Long.class, edu_courses.class);
	
	public static edu_courses findById(int Id) {
        return edu_courses.find.where().eq("course_id", Id).findUnique();
    }	
	
	public static List<edu_courses> findByName(String name) {
        return edu_courses.find.where().eq("parent_id", 0).ilike("name", name+"%").findList();
    }		
	
	public static Object[] findByUserDeviceId(String userDeviceId){
		String s="SELECT edu_courses.* FROM edu_coursesstudents LEFT Join edu_courses on edu_coursesstudents.course_id=edu_courses.course_id LEFT Join edu_users on edu_coursesstudents.user_id=edu_users.user_id Where edu_users.user_device_id='"+userDeviceId+"'";
		return Ebean.createSqlQuery(s).findList().toArray();
				
	}
	
	public static List<edu_courses> listLectures(int Id) {
        return edu_courses.find.where().eq("parent_id", Id).findList();
    }	
	
    public static boolean changeActiveStatusById(int Id, boolean is_active) {
    	try
    	{
    		edu_courses course = edu_courses.findById(Id);
	    	if (is_active == true)
	    		course.is_active = 1;
	    	else 
	    		course.is_active = 0;    
	    	course.save();	    	
	 		return true;
 		}
 		catch (Exception ex)	
 		{
 			return false;
 		}    	
    }	
    
    public static boolean createLecture(int course_id,int start_date, int end_date) {
    	//Copy from course and save it as lecture
    	try
    	{
	    	edu_courses ec = new edu_courses();
	    	ec.parent_id = course_id;
	    	ec.start_date = start_date;
	    	ec.end_date = end_date;
	    	ec.is_active = 1;
	    	ec.save();
	    	return true;
    	}
    	catch (Exception ex)
    	{
    		return false;
    	}
    }  
    
    public static boolean addTagToCourse(int course_id, String tag) {
	    edu_tags et = edu_tags.findByName(tag);
	    edu_tagscourses etc = new edu_tagscourses();
	    if (et != null)
	    {    		
	    	etc.tag_id = et.tag_id;
	    }
	    else
	    {
	    	edu_tags nt = new edu_tags();
	    	nt.tag = tag.trim();
	    	nt.creation_date = System.currentTimeMillis()/1000;
	    	nt.save();
	    	    		
	    	etc.tag_id = nt.tag_id;    		
	    }    	
		etc.course_id = course_id;
		etc.vote = 0;
		etc.added_by_user_id = 0;
		etc.save();
		return true;
    }
    
    public static boolean likeCourseTag(int course_id, int tag_id)
    {
    	try
    	{
	    	edu_tagscourses c = edu_tagscourses.find.where().eq("course_id", course_id).eq("tag_id",tag_id).findUnique();
	    	if (c.relation_id > 0)
	    	{
	    		c.vote += 1;
	    		c.save();
	    	}
	    	return true;
    	}
    	catch (Exception ex)
    	{
    		return false;
    	}
    }
    
    public static boolean dislikeCourseTag(int course_id, int tag_id)
    {
    	try
    	{
	    	edu_tagscourses c = edu_tagscourses.find.where().eq("course_id", course_id).eq("tag_id",tag_id).findUnique();
	    	if (c.relation_id > 0)
	    	{
	    		c.vote -= 1;
	    		c.save();
	    	}
	    	return true;
    	}
    	catch (Exception ex)
    	{
    		return false;
    	}    	
    }
    
    //retrieve student courses method start
    public static List<edu_coursesstudents> findCoursesStudents(int userid) {
    	
    	 return edu_coursesstudents.find.where().eq("user_id",userid).findList(); 	
    }	
  //retrieve student courses method finish
	
    @Override
	public long getId() {
		return course_id;
	}

	@Override
	public String getDescription() {
		if(description == null){
			return "";
		}
		return description;
	}

	@Override
	public String getName() {
		if(name == null){
			return "";
		}
		return name;
	}    
    
}
